<?php


namespace App\Services\Api;
use Elasticsearch\ClientBuilder;
use Illuminate\Support\Facades\Log;

/**
 * EsService
 * created on 2020/8/7 13:26
 * created by chengzhigang
 */
class EsService extends ApiService
{
    private $EsClient;
    /**
     * @var
     */
    private $index;
    public function __construct($index='orders'){
        $this->index = $index;
    }

    public function search($page=1,$per_page=10,$order="created_at",$sort="desc",$order_status=0,$keywords="",$filter=""){
        $params = $this->getParams($filter,$keywords,$order_status,$page,$per_page,$order,$sort);
        return app('es')->search($params);
    }

    public function getParams($filter="",$keywords="",$order_status=0,$page=1,$perPage=10,$order="created_at",$sort="desc"){
        $params = [
            'index' => $this->index,
            'body'  => [
                'from'  => ($page - 1) * $perPage, // 通过当前页数与每页数量计算偏移值
                'size'  => $perPage,
                'sort'  => [
                    $order  => [
                        'order' => $sort
                    ]
                ],
            ],
        ];
        //订单状态
        if($order_status){
            $params['body']['query']['bool']['filter']['term']['order_status'] = $order_status;
        }
        //关键字
        if($keywords){
            $keywords = array_filter(explode(' ', $keywords));
            $params['body']['query']['bool']['must'] = [];
            foreach($keywords as $keyword){
                $params['body']['query']['bool']['must'][] = [
                    'multi_match' => [
                        'query'  => $keyword,
                        'fields' => [
                            'user_name^2',
                            'consignee^2',
                            'store_name^2',
                            'addresses',
                            'goodsName',
                            'goodsSpec',
                            'properties_value'
                        ],
                    ],
                ];
            }
            $params['body']['aggs'] = [
                'properties' => [
                    'nested' => [
                        'path' => 'properties',
                    ],
                    'aggs'   => [
                        'properties' => [
                            'terms' => [
                                'field' => 'properties.name',
                            ],
                            // 第三层聚合
                            'aggs'  => [
                                // 聚合的名称
                                'value' => [
                                    'terms' => [
                                        'field' => 'properties.value',
                                    ],
                                ],
                            ],
                        ],
                    ],
                ]
            ];
        }
        if($filter){
            $params['body']['query']['bool']['filter'][] = [
                'nested' => [
                    'path'  => 'properties',
                    'query' => [
                        // 将原来的两个 term 查询改成一个
                        ['term' => ['properties.search_value' => $filter]],
                    ],
                ],
            ];
        }
        return $params;
    }

    public function getDoc($params){
        return $this->EsClient->get($params);
    }

    public function updateDoc($params){
        return $this->EsClient->update($params);
    }

    public function indexDoc($params){
        return $this->EsClient->index($params);
    }
}
